<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Faker;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
            ContactSeeder::class,
            ContactMethodSeeder::class,
            GroupSeeder::class,
        ]);
        $faker = Faker\Factory::create();
        $contacts = DB::table('contacts')->get();
        $groups = DB::table('groups')->get();
        $methods = DB::table('contact_methods')->get();
        foreach($contacts as $contact){
            if($faker->randomElement([1,2,3])!=2) continue; // get one-third opportunity
            $count = $faker->randomElement([1,2,3]);
            $faker->unique(true);
            for($i=0;$i<$count;$i++){
                $group = $faker->unique()->randomElement($groups->toArray());
                DB::table('contact_group')->insert([
                    'contact_id'=>$contact->id,
                    'group_id'=>$group->id
                ]);
            }
        }
        function getRecord($name): string
        {
            $faker = Faker\Factory::create();
            return match ($name) {
                'Email' => $faker->unique()->email,
                default => $faker->unique()->phoneNumber(),
            };
        }
        foreach ($contacts as $contact){
            $count = $faker->randomElement([1,2,3,4]);
            for($i=0;$i<$count;$i++){
                $method = $faker->randomElement($methods->toArray());
                DB::table('contact_records')->insert([
                    'contact_id'=>$contact->id,
                    'contact_method_id'=>$method->id,
                    'record'=> getRecord($method->name)
                    ]);
            }
        }
    }
}
